package com.arethusa;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.xml.serializer.DOMSerializer;
import org.apache.xml.serializer.Method;
import org.apache.xml.serializer.OutputPropertiesFactory;
import org.apache.xml.serializer.Serializer;
import org.apache.xml.serializer.SerializerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import simpleorm.core.SConnection;
import simpleorm.core.SPreparedStatement;
import simpleorm.core.SResultSet;
import simpleorm.core.SValidationException;

import com.Ostermiller.util.CSVParser;
import com.Ostermiller.util.CSVPrinter;
import com.Ostermiller.util.LabeledCSVParser;
import com.arethusa.langebaandb.CategorieBean;
import com.arethusa.langebaandb.FunktieBean;
import com.arethusa.langebaandb.LeeftijdsbepalingenBean;
import com.arethusa.langebaandb.ProcesverbaalBean;
import com.arethusa.langebaandb.ProgrammaBean;
import com.arethusa.langebaandb.ProgrammanummersBean;
import com.arethusa.langebaandb.StartBean;
import com.arethusa.langebaandb.StartkostenBean;
import com.arethusa.langebaandb.Vereniging;
import com.arethusa.langebaandb.VerenigingBean;
import com.arethusa.langebaandb.WedstrijdjuryBean;

public class Backup extends JPanel {

	private static final long serialVersionUID = 1L;
	private JTextField lenexUitslagTextField = null;
	private JButton lenexUitslagMakenButton = null;
	private JButton zoekLenexUitslagButton = null;
	private JButton zoekBackupMakenButton = null;
	private JButton backupMakenButton = null;
	private JPanel jPanel = null;
	private JTextField backupMakenTextField = null;
	private JTextField backupTerugzettenTextField = null;
	private JButton zoekBackupTerugzettenButton = null;
	private JButton backupTerugzettenButton = null;
	private List<CategorieBean> cats = new ArrayList<CategorieBean>(); 
	private JLabel backupMakenLabel = null;
	private JLabel backupTerugzettenLabel = null;
	private JLabel lenexUitslagLabel = null;
	
	/*
	 *  Zoek folder voor LENEX uitslag bestand
	 */
	private class ZoekLenexUitslagButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
		    JFileChooser chooser = new JFileChooser();
		    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		    chooser.setSelectedFile(new File(getBackupMakenTextField().getText()));
		    int returnVal = chooser.showOpenDialog(jPanel);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		    	getLenexUitslagTextField().setText(chooser.getSelectedFile().getPath()+"\\LenexUitslag.lef");
		    }
		}
		
	}

	/*
	 *  Maken van een LENEX 3.0 Uitslag bestand
	 */

	/*
	 * Exporteer LENEX uitslag
	 */
	// Temporary memory objects
	private class Event {
		public Integer prognr;
		public String suffix;
		public Long eventid;
		
		public Event(Integer prognr, String suffix, Long eventid) {
			this.prognr = prognr;
			this.suffix = suffix;
			this.eventid = eventid;
		}
	}
	private class EndResult {
		public String startnr;
		public Long eventid;
		public Long resultid;
		public Long place;
		
		public EndResult(String startnr, Long eventid, Long resultid, Long place) {
			this.startnr = startnr;
			this.eventid = eventid;
			this.resultid = resultid;
			this.place = place;
		}
	}

	private class LenexUitslagMakenButtonActionListener implements ActionListener {
		List<Event> events = new ArrayList<Event>();
		List<EndResult> results = new ArrayList<EndResult>();
		
		// Exporteer LENEX uitslag bestand
		@SuppressWarnings("deprecation")
		public void actionPerformed(ActionEvent arg0) {
			Document document = null;
			Element el1 = null;
			Element el2 = null;
			Element el3 = null;
			Element el4 = null;
			Element el5 = null;
			Element el6 = null;
			Element el7 = null;
			Element el8 = null;
			Element el9 = null;
			Element el10 = null;
			// Collect all rankings first
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT a.ProgNr, a.Suffix," +
						" StartNr, Eindtijdinsec" +
						" FROM Programma a LEFT JOIN Start b ON (a.ProgNr = b.ProgNr AND a.Suffix = b.Suffix) " +
						" ORDER BY ProgNr, Suffix, Eindtijdinsec");
				Long resultid = new Long(5000L);
				Long eventid = new Long(1000L);
				Long place = new Long(0L);
				Integer prgnr = 0;
				String sfx = "";
				while( res.next() ) {
					if( ! (prgnr == res.getInt("ProgNr") && sfx.equalsIgnoreCase(res.getString("Suffix"))) ) {
						eventid++;
						prgnr = res.getInt("ProgNr");
						sfx = res.getString("Suffix");
						events.add(new Event(prgnr, sfx, eventid));
						place = 0L;
					}
					if( res.getString("StartNr") != null ) {
						Double etis = res.getDouble("Eindtijdinsec");
						if( etis == null ) {
							place++;
						} else if( etis >= 10000.0 ) place=32767L; else place++;
						results.add(new EndResult(res.getString("StartNr"), 
								eventid, ++resultid, place));
					}
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			if( ! hasBegun ) { SConnection.commit(); }
			// Create the LENEX xml
			try {
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder();
				document = builder.newDocument();
				// <LENEX>
				Element root = document.createElement("LENEX");
				root.setAttribute("version", "3.0");
				document.appendChild(root);
				// <LENEX><CONSTRUCTOR>
				el1 = document.createElement("CONSTRUCTOR");
				el1.setAttribute("name", "OLOWUS");
				el1.setAttribute("version", "1.0");
				// <LENEX><CONSTRUCTOR><CONTACT>
				el2 = document.createElement("CONTACT");
				el2.setAttribute("name", "Erik Pijl");
				el2.setAttribute("email", "e_pijl@hotmail.com");
				el1.appendChild(el2);
				root.appendChild(el1);
				// <LENEX><MEETS<MEET>
				el1 = document.createElement("MEETS");
				el2 = document.createElement("MEET");
				Date d = null;
				String weddate = null;
				// Get Wedstrijdgegevens from database
				hasBegun = SConnection.getConnection().hasBegun();
				if( ! hasBegun ) { SConnection.begin(); }
				JDBCcon = SConnection.getBegunDBConnection();
				try {
					Statement stmt = JDBCcon.createStatement();
					ResultSet res = stmt.executeQuery("SELECT * FROM Procesverbaal ");
					Calendar c = Calendar.getInstance();
					while( res.next() ) {
						d = res.getDate("WedstrijdDatum", c);
						SimpleDateFormat dfmt = new SimpleDateFormat("yyyy-MM-dd");
						weddate = dfmt.format(d).toString();
					}
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null,
							e.getMessage()+e.getCause(),
							"SQL fout", JOptionPane.ERROR_MESSAGE);
				}
				if( ! hasBegun ) { SConnection.commit(); }
				el2.setAttribute("name", "Harense Smid Zwemrace");
				el2.setAttribute("city", "Oss");
				el2.setAttribute("nation", "NED");
				el2.setAttribute("organizer", "Arethusa");
				// <LENEX><MEETS><MEET><AGEDATE>
				el3 = document.createElement("AGEDATE");
				el3.setAttribute("type", "CAN.FNQ");
				el3.setAttribute("value", (1900+d.getYear())+"-07-01");
				el2.appendChild(el3);
//				// <LENEX><MEETS><MEET><FEES>
//				el3 = document.createElement("FEES");
//				// <LENEX><MEETS><MEET><FEES><FEE>
//				el4 = document.createElement("FEE");
//				el4.setAttribute("currency", "EUR");
//				el4.setAttribute("type", "ATHLETE");
//				el4.setAttribute("value", pps);
//				el3.appendChild(el4);
//				el2.appendChild(el3);
				// <LENEX><MEETS><MEET><POOL>
				el3 = document.createElement("POOL");
				el3.setAttribute("name", "Gouden Ham");
				el3.setAttribute("type", "LAKE");
				el2.appendChild(el3);
				// <LENEX><MEETS><MEET><CLUBS>
				el3 = document.createElement("CLUBS");
				// Get all starts from database
				hasBegun = SConnection.getConnection().hasBegun();
				if( ! hasBegun ) { SConnection.begin(); }
				JDBCcon = SConnection.getBegunDBConnection();
				try {
					Statement stmt = JDBCcon.createStatement();
					ResultSet res = stmt.executeQuery("SELECT a.ProgNr, a.Suffix," +
							" b.StartNr, b.Naam, a.Sex, b.Depotnr," +
							" b.Vereniging, b.Eindtijd, b.Dis" +
							" FROM Programma a, Start b " +
							" WHERE a.ProgNr=b.ProgNr AND a.Suffix=b.Suffix " +
							" ORDER BY Vereniging, Naam");
					Long athleteid = new Long(3000L);
					String depotnr = "";
					String startnr = "";
					Integer prgnr = 0;
					String sfx = "";
					while( res.next() ) {
						if( ! depotnr.equalsIgnoreCase(res.getString("Depotnr"))) {
							// <LENEX><MEETS><MEET><CLUBS><CLUB>
							el4 = document.createElement("CLUB");
							el4.setAttribute("code", res.getString("Depotnr"));
							el4.setAttribute("name", res.getString("Vereniging"));
							el4.setAttribute("nation", "NED");
							el4.setAttribute("region", res.getString("Depotnr").substring(0, 2));
							el4.setAttribute("type", "CLUB");
							// <LENEX><MEETS><MEET><CLUBS><CLUB><ATHLETES>
							el5 = document.createElement("ATHLETES");
							startnr = "";
							depotnr = res.getString("Depotnr");
						}
						if( ! startnr.equalsIgnoreCase(res.getString("StartNr"))) {
							// <LENEX><MEETS><MEET><CLUBS><CLUB><ATHLETES><ATHLETE>
							el6 = document.createElement("ATHLETE");
							String by = res.getString("StartNr").substring(0, 4);
							el6.setAttribute("athleteid", (++athleteid).toString());
							el6.setAttribute("birthdate", by+"-01-01");
							el6.setAttribute("firstname", "");
							el6.setAttribute("lastname", res.getString("Naam"));
							String sex = res.getString("Sex");
							String gender = (sex.equalsIgnoreCase("H")?"M":"F");
							el6.setAttribute("gender", gender);
							el6.setAttribute("license", res.getString("StartNr"));
							el6.setAttribute("nation", "NED");
							// <LENEX><MEETS><MEET><CLUBS><CLUB><ATHLETES><ATHLETE><RESULTS>
							el7 = document.createElement("RESULTS");
							startnr = res.getString("StartNr");
						}
						prgnr = res.getInt("ProgNr");
						sfx = res.getString("Suffix");
						// <LENEX><MEETS><MEET><CLUBS><CLUB><ATHLETES><ATHLETE><RESULTS><RESULT>
						el8 = document.createElement("RESULT");
						el8.setAttribute("eventid", getEventId(prgnr, sfx).toString());
						el8.setAttribute("resultid", getResultId(prgnr, sfx, startnr).toString());
						String et = res.getString("Eindtijd");
						if( et == null ) {
							el8.setAttribute("swimtime", "00:00:00.00");
							el8.setAttribute("status", "DNS");
						} else {
							if( et.length()==8 ) et = "00:"+et;
							if( et.length()==10 ) et = "0"+et;
							if( et.equalsIgnoreCase("AGZK") ) {
								el8.setAttribute("swimtime", "00:00:00.00");
								el8.setAttribute("status", "DNS");
							} else if( et.equalsIgnoreCase("NA") ) {
								el8.setAttribute("swimtime", "00:00:00.00");
								el8.setAttribute("status", "DNF");
							} else if( et.equalsIgnoreCase("AGMK") ) {
								el8.setAttribute("swimtime", "00:00:00.00");
								el8.setAttribute("status", "SICK");
							} else {
								el8.setAttribute("swimtime", et);
								if( ! res.getString("Dis").equalsIgnoreCase("") ) {
									el8.setAttribute("status", "DSQ");
								}
							}
						}
						el7.appendChild(el8);
						el6.appendChild(el7);
						el5.appendChild(el6);
						el4.appendChild(el5);
						el3.appendChild(el4);

					}
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null,
							e.getMessage()+e.getCause(),
							"SQL fout", JOptionPane.ERROR_MESSAGE);
				}
				if( ! hasBegun ) { SConnection.commit(); }
				el2.appendChild(el3);
				// <LENEX><MEETS><MEET><SESSIONS>
				el3 = document.createElement("SESSIONS");
				// <LENEX><MEETS><MEET><SESSIONS><SESSION>
				el4 = document.createElement("SESSION");
				el4.setAttribute("date",weddate);
				el4.setAttribute("number","1");
				// <LENEX><MEETS><MEET><CLUBS><CLUB><EVENTS>
				el5 = document.createElement("EVENTS");
				// Get Programma from database. Each programma nummer is an EVENT.
				hasBegun = SConnection.getConnection().hasBegun();
				if( ! hasBegun ) { SConnection.begin(); }
				JDBCcon = SConnection.getBegunDBConnection();
				Long eventid = null;
				try {
					Statement stmt = JDBCcon.createStatement();
					ResultSet res = stmt.executeQuery("SELECT * FROM Programma " +
							" WHERE Type<>'P'");
					Integer number = new Integer(0);
					while( res.next() ) {
						// <LENEX><MEETS><MEET><SESSIONS><SESSION><EVENTS><EVENT>
						el6 = document.createElement("EVENT");
						eventid = getEventId(res.getInt("ProgNr"), res.getString("Suffix"));
						el6.setAttribute("eventid", eventid.toString() );
						String sex = res.getString("Sex");
						String gender = (sex.equalsIgnoreCase("H")?"M":(sex.equalsIgnoreCase("D")?"F":"A"));
						el6.setAttribute("gender", gender);
						el6.setAttribute("round", "TIM");
						el6.setAttribute("number", (++number).toString());
						// <LENEX><MEETS><MEET><SESSIONS><SESSION><EVENTS><EVENT><SWIMSTYLE>
						el7 = document.createElement("SWIMSTYLE");
						el7.setAttribute("distance", new Integer(res.getInt("Afstand")).toString());
						String slag = res.getString("Slag");
						String stroke = (slag.equalsIgnoreCase("school")?"BREAST":
							(slag.equalsIgnoreCase("rug")?"BACK":
							(slag.equalsIgnoreCase("vrij")?"FREE":"FLY"
							)));
						el7.setAttribute("stroke", stroke);
						el7.setAttribute("relaycount", "1");
						el6.appendChild(el7);
						// <LENEX><MEETS><MEET><SESSIONS><SESSION><EVENTS><EVENT><AGEGROUPS>
						el7 = document.createElement("AGEGROUPS");
						// <LENEX><MEETS><MEET><SESSIONS><SESSION><EVENTS><EVENT><AGEGROUPS><AGEGROUP>
						String pvan = res.getString("LeeftijdVan");
						String ptm = res.getString("LeeftijdTM");
						el8 = document.createElement("AGEGROUP");
						el8.setAttribute("agegroupid", "1");
						el8.setAttribute("agemin", getAgeMin(pvan, sex).toString());
						el8.setAttribute("agemax", getAgeMax(ptm, sex).toString());
						// <LENEX><MEETS><MEET><SESSIONS><SESSION><EVENTS><EVENT><AGEGROUPS><AGEGROUP><RANKINGS>
						el9 = document.createElement("RANKINGS");
						for (EndResult r : results) {
							if( r.eventid == eventid ) {
								// <LENEX><MEETS><MEET><SESSIONS><SESSION><EVENTS><EVENT><AGEGROUPS><AGEGROUP><RANKINGS><RANKING>
								el10 = document.createElement("RANKING");
								el10.setAttribute("place", r.place.toString());
								el10.setAttribute("resultid", r.resultid.toString());
								el9.appendChild(el10);
							}
						}
						el8.appendChild(el9);
						el7.appendChild(el8);
						el6.appendChild(el7);
						el5.appendChild(el6);
					}
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null,
							e.getMessage()+e.getCause(),
							"SQL fout", JOptionPane.ERROR_MESSAGE);
				}
				if( ! hasBegun ) { SConnection.commit(); }
				
				el4.appendChild(el5);
				el3.appendChild(el4);
				el2.appendChild(el3);
				el1.appendChild(el2);
				root.appendChild(el1);
				
				
				// Ready
				try
				{
					//print
					Properties props = OutputPropertiesFactory.getDefaultMethodProperties(Method.XML);
					props.setProperty("indent","yes");
					props.setProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2");
					FileOutputStream fo = new FileOutputStream(new File(getLenexUitslagTextField().getText()));
					Serializer serializer = SerializerFactory.getSerializer(props);
					serializer.setOutputStream(fo);
					DOMSerializer dser = serializer.asDOMSerializer();
					dser.serialize(document);
					fo.flush();
					fo.close();

				} catch(IOException ie) {
				    ie.printStackTrace();
				}
				// Prepare the output file
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"XML fout", JOptionPane.ERROR_MESSAGE);
			}
		}

		private Long getResultId(Integer prgnr, String sfx, String startnr) {
			Long e = getEventId(prgnr, sfx);
			for (EndResult r : results) {
				if( r.eventid == e && r.startnr.equalsIgnoreCase(startnr) ) {
					return r.resultid;
				}
			}
			return null;
		}

		private Long getEventId(Integer prgnr, String sfx) {
			for(Event e : events) {
				if(e.prognr == prgnr && e.suffix.equalsIgnoreCase(sfx)) {
					return e.eventid;
				}
			}
			return null;
		}
	
	}
	
	/*
	 * Zoek plek voor backup bestanden
	 */
	private class ZoekBackupMakenButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
		    JFileChooser chooser = new JFileChooser();
		    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		    chooser.setSelectedFile(new File(getBackupMakenTextField().getText()));
		    int returnVal = chooser.showOpenDialog(jPanel);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		    	getBackupMakenTextField().setText(chooser.getSelectedFile().getPath());
		    }
		}
		
	}

	/*
	 * Creeer backup bestanden
	 */
	private class BackupMakenButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ExportStarts();
			ExportCategorien();
			ExportLeeftijdsbepalingen();
			ExportFunkties();
			ExportStartkosten();
			ExportProgramma();
			ExportProgrammanummers();
			ExportProcesVerbaal();
			ExportJury();
		}
	}
	
	/*
	 * Bepaal plaats van backup bestanden
	 */
	private class ZoekBackupTerugzettenButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
		    JFileChooser chooser = new JFileChooser();
		    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		    chooser.setSelectedFile(new File(getBackupMakenTextField().getText()));
		    int returnVal = chooser.showOpenDialog(jPanel);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
		    	getBackupTerugzettenTextField().setText(chooser.getSelectedFile().getPath());
		    }
		}
		
	}
	private class BackupTerugzettenButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ImportProgramma();
			ImportProgrammanummers();
			ImportStarts();
			ImportCategorien();
			ImportLeeftijdsbepalingen();
			ImportFunkties();
			ImportStartkosten();
			ImportProcesVerbaal();
			ImportJury();
		}
	}
	
	private void ExportStarts() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		try {
			CSVPrinter lcsvp = new CSVPrinter(new FileOutputStream(getBackupMakenTextField().getText()+"\\exportstarts.csv"));
			lcsvp.changeDelimiter(';');
			String[] lbls = {"ProgNr","Suffix","Titel","CapKleur","CapNr",
					"Vast","StartNr","Naam","DepotNr","Vereniging","Inschrijver",
					"Eindtijd","Dis","EindtijdInSec",""};
			lcsvp.println(lbls);
			
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT a.ProgNr, a.Suffix, a.Titel," +
						" b.CapKleur, b.CapNr, b.Vast, b.StartNr, b.Naam, b.Depotnr," +
						" b.Vereniging, b.Inschrijver, b.Eindtijd, b.Dis, b.Eindtijdinsec" +
						" FROM Programma a, Start b " +
						" WHERE a.ProgNr=b.ProgNr AND a.Suffix=b.Suffix " +
						" ORDER BY ProgNr, Suffix, Eindtijdinsec");
				while( res.next() ) {
					lcsvp.print(res.getInt("Prognr")+"");
					lcsvp.print(res.getString("Suffix"));
					lcsvp.print(res.getString("Titel"));
					lcsvp.print(res.getString("Capkleur"));
					lcsvp.print(res.getInt("Capnr")+"");
					lcsvp.print(res.getInt("Vast")+"");
					lcsvp.print(res.getString("Startnr"));
					lcsvp.print(res.getString("Naam"));
					lcsvp.print(res.getString("Depotnr"));
					lcsvp.print(res.getString("Vereniging"));
					lcsvp.print(res.getString("Inschrijver"));
					lcsvp.print(res.getString("Eindtijd"));
					lcsvp.print(res.getString("Dis"));
					if(res.getBigDecimal("Eindtijdinsec") != null ) 
						lcsvp.print(res.getBigDecimal("Eindtijdinsec").toString());
					else
						lcsvp.print("");
					lcsvp.println();
					nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			if( ! hasBegun ) { SConnection.commit(); }
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" starts opgeslagen.",
					"Export", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void ExportCategorien() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		try {
			CSVPrinter lcsvp = new CSVPrinter(new FileOutputStream(getBackupMakenTextField().getText()+"\\exportcat.csv"));
			lcsvp.changeDelimiter(';');
			String[] lbls = {"Cat","Sex","Van","Tem"};
			lcsvp.println(lbls);
			
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT Cat, Sex, Van, Tem" +
						" FROM Categorie " +
						" ORDER BY Cat, Sex");
				while( res.next() ) {
					lcsvp.print(res.getString("Cat"));
					lcsvp.print(res.getString("Sex"));
					lcsvp.print(res.getInt("Van")+"");
					lcsvp.print(res.getInt("Tem")+"");
					lcsvp.println();
					nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			if( ! hasBegun ) { SConnection.commit(); }
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" categorien opgeslagen.",
					"Export", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void ExportLeeftijdsbepalingen() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		try {
			CSVPrinter lcsvp = new CSVPrinter(new FileOutputStream(getBackupMakenTextField().getText()+"\\exportlftb.csv"));
			lcsvp.changeDelimiter(';');
			String[] lbls = {"Leeftijd","MaxAfstand", "MaxStarts"};
			lcsvp.println(lbls);
			
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT Leeftijd, MaxAfstand, MaxStarts" +
						" FROM leeftijdsbepalingen " +
						" ORDER BY leeftijd");
				while( res.next() ) {
					lcsvp.print(res.getInt("Leeftijd")+"");
					lcsvp.print(res.getInt("MaxAfstand")+"");
					lcsvp.print(res.getInt("MaxStarts")+"");
					lcsvp.println();
					nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			if( ! hasBegun ) { SConnection.commit(); }
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" leefijdsbepalingen opgeslagen.",
					"Export", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void ExportFunkties() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		try {
			CSVPrinter lcsvp = new CSVPrinter(new FileOutputStream(getBackupMakenTextField().getText()+"\\exportfunkties.csv"));
			lcsvp.changeDelimiter(';');
			String[] lbls = {"Code","Naam", "Seqnr", "Blok"};
			lcsvp.println(lbls);
			
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT Code, Naam, Seqnr, Blok" +
						" FROM funktie " +
						" ORDER BY Seqnr");
				while( res.next() ) {
					lcsvp.print(res.getString("Code"));
					lcsvp.print(res.getString("Naam"));
					lcsvp.print(res.getInt("Seqnr")+"");
					lcsvp.print(res.getString("Blok"));
					lcsvp.println();
					nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			if( ! hasBegun ) { SConnection.commit(); }
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" funkties opgeslagen.",
					"Export", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void ExportStartkosten() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		try {
			CSVPrinter lcsvp = new CSVPrinter(new FileOutputStream(getBackupMakenTextField().getText()+"\\exportkosten.csv"));
			lcsvp.changeDelimiter(';');
			String[] lbls = {"LabelStart1", "KostenStart1", "LabelStart2", "KostenStart2",
							 "LabelStart3", "KostenStart3", "LabelStart4", "KostenStart4",
							 "LabelStart5","KostenStart5"};
			lcsvp.println(lbls);
			
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT LabelStart1, KostenStart1," +
						" LabelStart2, KostenStart2, LabelStart3, KostenStart3," +
						" LabelStart4, KostenStart4, LabelStart5, KostenStart5" +
						" FROM startkosten");
				while( res.next() ) {
					lcsvp.print(res.getString("LabelStart1"));
					lcsvp.print(res.getBigDecimal("KostenStart1")+"");
					lcsvp.print(res.getString("LabelStart2"));
					lcsvp.print(res.getBigDecimal("KostenStart2")+"");
					lcsvp.print(res.getString("LabelStart3"));
					lcsvp.print(res.getBigDecimal("KostenStart3")+"");
					lcsvp.print(res.getString("LabelStart4"));
					lcsvp.print(res.getBigDecimal("KostenStart4")+"");
					lcsvp.print(res.getString("LabelStart5"));
					lcsvp.print(res.getBigDecimal("KostenStart5")+"");
					lcsvp.println();
					nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			if( ! hasBegun ) { SConnection.commit(); }
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" startkosten opgeslagen.",
					"Export", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void ExportProgramma() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		try {
			CSVPrinter lcsvp = new CSVPrinter(new FileOutputStream(getBackupMakenTextField().getText()+"\\exportwedprog.csv"));
			lcsvp.changeDelimiter(';');
			String[] lbls = {"ProgNr","Suffix","Titel","CapKleur","CapKleurKNZB",
					"Afstand","Slag","Sex","LeeftijdVan","LeeftijdTM"};
			lcsvp.println(lbls);
			
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT ProgNr,Suffix,Titel,CapKleur,CapKleurKNZB,"+
						"Afstand,Slag,Sex,LeeftijdVan,LeeftijdTM" +
						" FROM programma " +
						" ORDER BY ProgNr,Suffix");
				while( res.next() ) {
					lcsvp.print(res.getInt("ProgNr")+"");
					lcsvp.print(res.getString("Suffix"));
					lcsvp.print(res.getString("Titel"));
					lcsvp.print(res.getString("CapKleur"));
					lcsvp.print(res.getString("CapKleurKNZB"));
					lcsvp.print(res.getInt("Afstand")+"");
					lcsvp.print(res.getString("Slag"));
					lcsvp.print(res.getString("Sex"));
					lcsvp.print(res.getString("LeeftijdVan"));
					lcsvp.print(res.getString("LeeftijdTM"));
					lcsvp.println();
					nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			if( ! hasBegun ) { SConnection.commit(); }
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" programmas opgeslagen.",
					"Export", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void ExportProgrammanummers() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		try {
			CSVPrinter lcsvp = new CSVPrinter(new FileOutputStream(getBackupMakenTextField().getText()+"\\exportprognrs.csv"));
			lcsvp.changeDelimiter(';');
			String[] lbls = {"ProgNr","Titel"};
			lcsvp.println(lbls);
			
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT ProgNr, Titel" +
						" FROM programmanummers " +
						" ORDER BY ProgNr");
				while( res.next() ) {
					lcsvp.print(res.getInt("ProgNr")+"");
					lcsvp.print(res.getString("Titel"));
					lcsvp.println();
					nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			if( ! hasBegun ) { SConnection.commit(); }
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" programmanummers opgeslagen.",
					"Export", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void ExportProcesVerbaal() {
		try {
			CSVPrinter lcsvp = new CSVPrinter(new FileOutputStream(getBackupMakenTextField().getText()+"\\exportprocesverbaal.csv"));
			lcsvp.changeDelimiter(';');
			String[] lbls = {"WedstrijdID","WedstrijdNaam","WedstrijdDatum","Organisatie","StartMethode","Tijdmeting",
					"WaterTemperatuur","Zwembad","Plaats","Baanlengte","Aanvang","Einde","Waarnemer","AfwezigMK",
					"AfwezigZK","Opmerkingen"};
			lcsvp.println(lbls);
			
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT *" +
						" FROM procesverbaal");
				if( res.next() ) {
					lcsvp.print(res.getInt("WedstrijdID")+"");
					lcsvp.print(res.getString("WedstrijdNaam"));
					SimpleDateFormat dfmt = new SimpleDateFormat("dd-MMM-yyyy");
					lcsvp.print(dfmt.format(res.getDate("WedstrijdDatum")));
					lcsvp.print(res.getString("Organisatie"));
					lcsvp.print(res.getString("StartMethode"));
					lcsvp.print(res.getString("Tijdmeting"));
					lcsvp.print(res.getBigDecimal("WaterTemperatuur").toPlainString());
					lcsvp.print(res.getString("Zwembad"));
					lcsvp.print(res.getString("Plaats"));
					lcsvp.print(res.getInt("Baanlengte")+"");
					lcsvp.print(res.getTime("Aanvang").toString());
					lcsvp.print(res.getTime("Einde").toString());
					lcsvp.print(res.getString("Waarnemer"));
					lcsvp.print(res.getInt("AfwezigMK")+"");
					lcsvp.print(res.getInt("AfwezigZK")+"");
					lcsvp.print(res.getString("Opmerkingen"));
					lcsvp.println();
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			SConnection.commit();
			
			lcsvp.close();
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		}
		
	}
	
	private void ExportJury() {
		//Open LabeledCVSParser
		Integer nrOfRecs = new Integer(0);
		try {
			CSVPrinter lcsvp = new CSVPrinter(new FileOutputStream(getBackupMakenTextField().getText()+"\\exportjury.csv"));
			lcsvp.changeDelimiter(';');
			String[] lbls = {"Funktie","Naam","Plaats","Bevoegdheid","Kloktijd","Code","BlokNr","SeqNr"};
			lcsvp.println(lbls);
			
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT Funktie,Naam,Plaats,Bevoegdheid,Kloktijd,Code,BlokNr,SeqNr" +
						" FROM wedstrijdjury " +
						" ORDER BY SeqNr");
				while( res.next() ) {
					lcsvp.print(res.getString("Funktie"));
					lcsvp.print(res.getString("Naam"));
					lcsvp.print(res.getString("Plaats"));
					lcsvp.print(res.getString("Bevoegdheid"));
					lcsvp.print(res.getString("Kloktijd"));
					lcsvp.print(res.getString("Code"));
					lcsvp.print(res.getString("BlokNr"));
					lcsvp.print(res.getInt("SeqNr")+"");
					lcsvp.println();
					nrOfRecs = new Integer(nrOfRecs.intValue() + 1);
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			if( ! hasBegun ) { SConnection.commit(); }
			
			JOptionPane.showMessageDialog(null,
					nrOfRecs.toString()+" juryleden opgeslagen.",
					"Export", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfRecs.toString()+e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Export", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void ImportJury() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SConnection.rawUpdateDB("DELETE FROM WedstrijdJury");
		SConnection.rawUpdateDB("ALTER TABLE WedstrijdJury AUTO_INCREMENT = 1");
		SConnection.commit();

		try {
			LabeledCSVParser lcsvp = new LabeledCSVParser(new CSVParser(new FileInputStream(getBackupTerugzettenTextField().getText()+"\\exportjury.csv")));
			lcsvp.changeDelimiter(';');

			while(lcsvp.getLine() != null) {

				WedstrijdjuryBean strt = new WedstrijdjuryBean();
				strt.setId(0L);
				strt.setFunktie(lcsvp.getValueByLabel("Funktie"));
				strt.setNaam(lcsvp.getValueByLabel("Naam"));
				strt.setPlaats(lcsvp.getValueByLabel("Plaats"));
				strt.setBevoegdheid(lcsvp.getValueByLabel("Bevoegdheid"));
				strt.setKloktijd(lcsvp.getValueByLabel("Kloktijd"));
				strt.setCode(lcsvp.getValueByLabel("Code"));
				strt.setBloknr(lcsvp.getValueByLabel("BlokNr"));
				strt.setSeqnr(Long.parseLong(lcsvp.getValueByLabel("SeqNr")));
				strt.create();

				nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
			}
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" juryleden binnengehaald en opgeslagen.",
					"Import", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+":"+e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void ImportProcesVerbaal() {
		//Open LabeledCVSParser
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SConnection.rawUpdateDB("DELETE FROM procesverbaal");
		SConnection.rawUpdateDB("ALTER TABLE procesverbaal AUTO_INCREMENT = 1");
		SConnection.commit();

		try {
			LabeledCSVParser lcsvp = new LabeledCSVParser(new CSVParser(new FileInputStream(getBackupTerugzettenTextField().getText()+"\\exportprocesverbaal.csv")));
			lcsvp.changeDelimiter(';');

			while(lcsvp.getLine() != null) {

				ProcesverbaalBean strt = new ProcesverbaalBean();
				strt.setWedstrijdid(new Integer(lcsvp.getValueByLabel("WedstrijdID")));
				strt.setWedstrijdnaam(lcsvp.getValueByLabel("WedstrijdNaam"));
				SimpleDateFormat wdtf = new SimpleDateFormat("dd-MMM-yyyy");
				Date wdt = wdtf.parse(lcsvp.getValueByLabel("WedstrijdDatum"));
				strt.setWedstrijddatum(wdt);
				strt.setOrganisatie(lcsvp.getValueByLabel("Organisatie"));
				strt.setStartmethode(lcsvp.getValueByLabel("StartMethode"));
				strt.setTijdmeting(lcsvp.getValueByLabel("Tijdmeting"));
				strt.setWatertemperatuur(new BigDecimal(lcsvp.getValueByLabel("WaterTemperatuur")));
				strt.setZwembad(lcsvp.getValueByLabel("Zwembad"));
				strt.setPlaats(lcsvp.getValueByLabel("Plaats"));
				strt.setBaanlengte(new Integer(lcsvp.getValueByLabel("Baanlengte")));
				wdtf = new SimpleDateFormat("HH:mm:ss");
				Time tm = new Time(wdtf.parse(lcsvp.getValueByLabel("Aanvang")).getTime());
				strt.setAanvang(tm);
				tm = new Time(wdtf.parse(lcsvp.getValueByLabel("Einde")).getTime());
				strt.setEinde(tm);
				strt.setWaarnemer(lcsvp.getValueByLabel("Waarnemer"));
				strt.setAfwezigmk(new Integer(lcsvp.getValueByLabel("AfwezigMK")));
				strt.setAfwezigzk(new Integer(lcsvp.getValueByLabel("AfwezigZK")));
				strt.setOpmerkingen(lcsvp.getValueByLabel("Opmerkingen"));
				strt.create();

			}
			
			JOptionPane.showMessageDialog(null,
					"Proces verbaal binnengehaald en opgeslagen.",
					"Import", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (ParseException e) {
			JOptionPane.showMessageDialog(null,
					"Wedstrijd datum is fout: "+e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void ImportProgrammanummers() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SConnection.rawUpdateDB("DELETE FROM programmanummers");
		SConnection.commit();

		try {
			LabeledCSVParser lcsvp = new LabeledCSVParser(new CSVParser(new FileInputStream(getBackupTerugzettenTextField().getText()+"\\exportprognrs.csv")));
			lcsvp.changeDelimiter(';');

			while(lcsvp.getLine() != null) {

				ProgrammanummersBean strt = new ProgrammanummersBean();
				strt.setPrognr(new Integer(lcsvp.getValueByLabel("ProgNr")));
				strt.setTitel(lcsvp.getValueByLabel("Titel"));
				strt.create();

				nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
			}
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" programmanummers binnengehaald en opgeslagen.",
					"Import", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void ImportProgramma() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SConnection.rawUpdateDB("DELETE FROM programma");
		SConnection.rawUpdateDB("DELETE FROM statistiekenperprogramma");
		SConnection.commit();
		try {
			LabeledCSVParser lcsvp = new LabeledCSVParser(new CSVParser(new FileInputStream(getBackupTerugzettenTextField().getText()+"\\exportwedprog.csv")));
			lcsvp.changeDelimiter(';');

			while(lcsvp.getLine() != null) {

				ProgrammaBean prg = new ProgrammaBean();
				prg.setPrognr(new Integer(lcsvp.getValueByLabel("ProgNr")));
				prg.setSuffix(lcsvp.getValueByLabel("Suffix"));
				prg.setTitel(lcsvp.getValueByLabel("Titel"));
				prg.setCapkleurknzb(lcsvp.getValueByLabel("CapKleurKNZB"));
				prg.setAfstand(new Integer(lcsvp.getValueByLabel("Afstand")));
				prg.setSlag(lcsvp.getValueByLabel("Slag"));
				prg.setSex(lcsvp.getValueByLabel("Sex"));
				prg.setLeeftijdvan(lcsvp.getValueByLabel("LeeftijdVan"));
				prg.setLeeftijdtm(lcsvp.getValueByLabel("LeeftijdTM"));
				prg.setCapkleur(lcsvp.getValueByLabel("CapKleur"));
				prg.create();
				nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
			}
			
			boolean hasBegun1 = SConnection.getConnection().hasBegun();
			if( ! hasBegun1 ) { SConnection.begin(); }
			SConnection.rawUpdateDB("INSERT INTO statistiekenperprogramma (ProgNr, Suffix,Titel) "+
					"SELECT ProgNr,Suffix,Titel FROM programma");
			SConnection.commit();
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" programma's binnengehaald en opgeslagen.",
					"Import", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void ImportLeeftijdsbepalingen() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SConnection.rawUpdateDB("DELETE FROM leeftijdsbepalingen");
		SConnection.commit();

		try {
			LabeledCSVParser lcsvp = new LabeledCSVParser(new CSVParser(new FileInputStream(getBackupTerugzettenTextField().getText()+"\\exportlftb.csv")));
			lcsvp.changeDelimiter(';');

			while(lcsvp.getLine() != null) {

				LeeftijdsbepalingenBean strt = new LeeftijdsbepalingenBean();
				strt.setLeeftijd(new Integer(lcsvp.getValueByLabel("Leeftijd")));
				strt.setMaxafstand(new Integer(lcsvp.getValueByLabel("MaxAfstand")));
				strt.setMaxstarts(new Integer(lcsvp.getValueByLabel("MaxStarts")));
				strt.create();

				nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
			}
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" leeftijdsbepalingen binnengehaald en opgeslagen.",
					"Import", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		}

	}

	private void ImportFunkties() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SConnection.rawUpdateDB("DELETE FROM funktie");
		SConnection.commit();

		try {
			LabeledCSVParser lcsvp = new LabeledCSVParser(new CSVParser(new FileInputStream(getBackupTerugzettenTextField().getText()+"\\exportfunkties.csv")));
			lcsvp.changeDelimiter(';');

			while(lcsvp.getLine() != null) {

				FunktieBean strt = new FunktieBean();
				strt.setCode(lcsvp.getValueByLabel("Code"));
				strt.setNaam(lcsvp.getValueByLabel("Naam"));
				strt.setSeqnr(new Long(lcsvp.getValueByLabel("Seqnr")));
				strt.setBlok(new Boolean(lcsvp.getValueByLabel("Blok")));
				strt.create();

				nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
			}
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" funkties binnengehaald en opgeslagen.",
					"Import", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		}

	}

	private void ImportStartkosten() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SConnection.rawUpdateDB("DELETE FROM startkosten");
		SConnection.commit();

		try {
			LabeledCSVParser lcsvp = new LabeledCSVParser(new CSVParser(new FileInputStream(getBackupTerugzettenTextField().getText()+"\\exportkosten.csv")));
			lcsvp.changeDelimiter(';');

			while(lcsvp.getLine() != null) {

				StartkostenBean strt = new StartkostenBean();
				strt.setWedstrijdid(1);
				strt.setLabelstart1(lcsvp.getValueByLabel("LabelStart1"));
				if( !lcsvp.getValueByLabel("KostenStart1").equalsIgnoreCase("null"))
					strt.setKostenstart1(new BigDecimal(lcsvp.getValueByLabel("KostenStart1")));
				strt.setLabelstart2(lcsvp.getValueByLabel("LabelStart2"));
				if( !lcsvp.getValueByLabel("KostenStart2").equalsIgnoreCase("null"))
					strt.setKostenstart2(new BigDecimal(lcsvp.getValueByLabel("KostenStart2")));
				strt.setLabelstart3(lcsvp.getValueByLabel("LabelStart3"));
				if( !lcsvp.getValueByLabel("KostenStart3").equalsIgnoreCase("null"))
					strt.setKostenstart3(new BigDecimal(lcsvp.getValueByLabel("KostenStart3")));
				strt.setLabelstart4(lcsvp.getValueByLabel("LabelStart4"));
				if( !lcsvp.getValueByLabel("KostenStart4").equalsIgnoreCase("null"))
					strt.setKostenstart4(new BigDecimal(lcsvp.getValueByLabel("KostenStart4")));
				strt.setLabelstart5(lcsvp.getValueByLabel("LabelStart5"));
				if( !lcsvp.getValueByLabel("KostenStart5").equalsIgnoreCase("null"))
					strt.setKostenstart5(new BigDecimal(lcsvp.getValueByLabel("KostenStart5")));
				strt.create();

				nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
			}
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" startkosten binnengehaald en opgeslagen.",
					"Import", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		}

	}

	private void ImportCategorien() {
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SConnection.rawUpdateDB("DELETE FROM categorie");
		SConnection.commit();

		try {
			LabeledCSVParser lcsvp = new LabeledCSVParser(new CSVParser(new FileInputStream(getBackupTerugzettenTextField().getText()+"\\exportcat.csv")));
			lcsvp.changeDelimiter(';');

			while(lcsvp.getLine() != null) {

				CategorieBean strt = new CategorieBean();
				strt.setCat(lcsvp.getValueByLabel("Cat"));
				strt.setSex(lcsvp.getValueByLabel("Sex"));
				strt.setVan(new Integer(lcsvp.getValueByLabel("Van")));
				strt.setTem(new Integer(lcsvp.getValueByLabel("Tem")));
				strt.create();

				nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
			}
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" categorien binnengehaald en opgeslagen.",
					"Import", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();

		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		}

	}

	private void ImportStarts() {
		String dnr;
		String ver;
		String ckl;
		//Open LabeledCVSParser
		Integer nrOfStarts = new Integer(0);
		Integer nrOfClubs = new Integer(0);
		boolean hasBegun = SConnection.getConnection().hasBegun();
		if( ! hasBegun ) { SConnection.begin(); }
		SConnection.rawUpdateDB("DELETE FROM start");
		SConnection.rawUpdateDB("DELETE FROM vereniging");
		SConnection.rawUpdateDB("ALTER TABLE start AUTO_INCREMENT = 1");
		SConnection.commit();
		try {
			LabeledCSVParser lcsvp = new LabeledCSVParser(new CSVParser(new FileInputStream(getBackupTerugzettenTextField().getText()+"\\exportstarts.csv")));
			lcsvp.changeDelimiter(';');
			while(lcsvp.getLine() != null) {
				StartBean strt = new StartBean();
				strt.setId(new Long(0));
				strt.setPrognr(new Integer(lcsvp.getValueByLabel("ProgNr")));
				strt.setSuffix(lcsvp.getValueByLabel("Suffix"));
				ckl = lcsvp.getValueByLabel("CapKleur");
				if( ckl.length()>25 ) ckl = ckl.substring(0, 25);
				strt.setCapkleur(ckl);
				String capnr=lcsvp.getValueByLabel("CapNr");
				if(capnr != null && capnr.length()>0 )strt.setCapnr(new Integer(capnr));
				strt.setVast(new Boolean((lcsvp.getValueByLabel("Vast")).equalsIgnoreCase("1")?true:false));
				ckl = lcsvp.getValueByLabel("StartNr");
				if( ckl.length() < 7 ) 
					ckl = "0000000".substring(0, 7-ckl.length()) + ckl;
				strt.setStartnr(ckl);
				strt.setNaam(lcsvp.getValueByLabel("Naam"));
				dnr = lcsvp.getValueByLabel("DepotNr");
				if( dnr.length() == 4 ) dnr = "0" + dnr;  
				strt.setDepotnr(dnr);
				ver = lcsvp.getValueByLabel("Vereniging");
				strt.setVereniging(ver);
				strt.setInschrijver(lcsvp.getValueByLabel("Inschrijver"));
				strt.setEindtijd(lcsvp.getValueByLabel("Eindtijd"));
				strt.setDis(lcsvp.getValueByLabel("Dis"));
				if(lcsvp.getValueByLabel("EindtijdInSec").length() != 0)
					strt.setEindtijdinsec(new BigDecimal(lcsvp.getValueByLabel("EindtijdInSec")));
				strt.create();
				nrOfStarts = new Integer(nrOfStarts.intValue() + 1);
				// Check of vereniging al bestaat in Vereniging tabel.
				// Zo niet, voeg toe. Als naam anders is, wijzig naam.
				boolean hasBegun1 = SConnection.getConnection().hasBegun();
				if( ! hasBegun1 ) { SConnection.begin(); }
				SPreparedStatement stmt = Vereniging.meta.select(
						"DepotNr="+dnr, "DepotNr");
				SResultSet res = stmt.execute();
				/// loop through the results
				if (res.hasNext()) {	// bestaat al
					VerenigingBean v = new VerenigingBean(((Vereniging)res.getRecord()));
			    	SConnection.commit();
					if( ! v.getNaam().equalsIgnoreCase(ver) ) {
						//naam is anders, dus update naam
//							System.out.println("Vereniging "+v.getNaam()+"("+dnr+") heet nu "+ver);
						v.setNaam(ver);
						v.update();
					}
				} else {				// is nieuw
			    	SConnection.commit();
					VerenigingBean v = new VerenigingBean();
					v.setDepotnr(new Integer(dnr));
					v.setNaam(lcsvp.getValueByLabel("Vereniging"));
//						System.out.println("Vereniging "+v.getNaam()+"("+dnr+") is nieuw.");
					v.create();
					nrOfClubs++;
				}
				
			}
			
			JOptionPane.showMessageDialog(null,
					nrOfStarts.toString()+" starts van "+
					nrOfClubs.toString()+" verenigingen binnengehaald en opgeslagen.",
					"Import", JOptionPane.INFORMATION_MESSAGE);
			lcsvp.close();
		} catch (FileNotFoundException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IllegalStateException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		} catch (SValidationException e) {
			JOptionPane.showMessageDialog(null,
					"Record "+nrOfStarts.toString()+e.getMessage(),
					"Import", JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 * This is the default constructor
	 */
	public Backup() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
		gridBagConstraints17.gridx = 0;
		gridBagConstraints17.fill = GridBagConstraints.BOTH;
		gridBagConstraints17.gridy = 2;
		this.setLayout(new BorderLayout());
		this.setSize(700, 109);
		this.add(getJPanel(), BorderLayout.NORTH);
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getLenexUitslagTextField() {
		if (lenexUitslagTextField == null) {
			lenexUitslagTextField = new JTextField();
		}
		return lenexUitslagTextField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getLenexUitslagMakenButton() {
		if (lenexUitslagMakenButton == null) {
			lenexUitslagMakenButton = new JButton();
			lenexUitslagMakenButton.setText("Maak uitslag");
			lenexUitslagMakenButton.setPreferredSize(new Dimension(130, 26));
			lenexUitslagMakenButton.addActionListener(new LenexUitslagMakenButtonActionListener());
		}
		return lenexUitslagMakenButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getZoekLenexUitslagButton() {
		if (zoekLenexUitslagButton == null) {
			zoekLenexUitslagButton = new JButton();
			zoekLenexUitslagButton.setText("Zoek");
			zoekLenexUitslagButton.addActionListener(new ZoekLenexUitslagButtonActionListener());
		}
		return zoekLenexUitslagButton;
	}

	/**
	 * This method initializes zoekBackupMakenButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getZoekBackupMakenButton() {
		if (zoekBackupMakenButton == null) {
			zoekBackupMakenButton = new JButton();
			zoekBackupMakenButton.setText("Zoek");
			zoekBackupMakenButton.addActionListener(new ZoekBackupMakenButtonActionListener());
		}
		return zoekBackupMakenButton;
	}

	/**
	 * This method initializes backupMakenButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBackupMakenButton() {
		if (backupMakenButton == null) {
			backupMakenButton = new JButton();
			backupMakenButton.setText("Maak backup");
			backupMakenButton.setPreferredSize(new Dimension(130, 26));
			backupMakenButton.addActionListener(new BackupMakenButtonActionListener());
		}
		return backupMakenButton;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.gridx = 0;
			gridBagConstraints31.anchor = GridBagConstraints.EAST;
			gridBagConstraints31.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints31.gridy = 2;
			lenexUitslagLabel = new JLabel();
			lenexUitslagLabel.setText("LENEX 3.0 uitslag maken");
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 0;
			gridBagConstraints21.anchor = GridBagConstraints.EAST;
			gridBagConstraints21.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints21.gridy = 1;
			backupTerugzettenLabel = new JLabel();
			backupTerugzettenLabel.setText("Backup terugzetten");
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.gridx = 0;
			gridBagConstraints12.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints12.anchor = GridBagConstraints.EAST;
			gridBagConstraints12.gridy = 0;
			backupMakenLabel = new JLabel();
			backupMakenLabel.setText("Backup maken");
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints11.gridy = 2;
			gridBagConstraints11.gridx = 3;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints10.gridy = 2;
			gridBagConstraints10.gridx = 2;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints9.gridy = 2;
			gridBagConstraints9.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints9.gridx = 1;
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints7.gridy = 1;
			gridBagConstraints7.gridx = 3;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.gridx = 2;
			gridBagConstraints6.gridy = 1;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridx = 1;
			gridBagConstraints5.gridy = 1;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.gridy = 0;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 3;
			gridBagConstraints2.gridy = 0;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints1.gridy = 0;
			gridBagConstraints1.gridx = 2;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getBackupMakenTextField(), gridBagConstraints3);
			jPanel.add(getZoekBackupMakenButton(), gridBagConstraints1);
			jPanel.add(getBackupMakenButton(), gridBagConstraints2);
			jPanel.add(getBackupTerugzettenTextField(), gridBagConstraints5);
			jPanel.add(getZoekBackupTerugzettenButton(), gridBagConstraints6);
			jPanel.add(getBackupTerugzettenButton(), gridBagConstraints7);
			jPanel.add(getLenexUitslagTextField(), gridBagConstraints9);
			jPanel.add(getZoekLenexUitslagButton(), gridBagConstraints10);
			jPanel.add(getLenexUitslagMakenButton(), gridBagConstraints11);
			jPanel.add(backupMakenLabel, gridBagConstraints12);
			jPanel.add(backupTerugzettenLabel, gridBagConstraints21);
			jPanel.add(lenexUitslagLabel, gridBagConstraints31);
		}
		return jPanel;
	}

	/**
	 * This method initializes backupMakenTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getBackupMakenTextField() {
		if (backupMakenTextField == null) {
			backupMakenTextField = new JTextField();
		}
		return backupMakenTextField;
	}

	/**
	 * This method initializes backupTerugzettenTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getBackupTerugzettenTextField() {
		if (backupTerugzettenTextField == null) {
			backupTerugzettenTextField = new JTextField();
		}
		return backupTerugzettenTextField;
	}

	/**
	 * This method initializes zoekBackupTerugzettenButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getZoekBackupTerugzettenButton() {
		if (zoekBackupTerugzettenButton == null) {
			zoekBackupTerugzettenButton = new JButton();
			zoekBackupTerugzettenButton.setText("Zoek");
			zoekBackupTerugzettenButton.addActionListener(new ZoekBackupTerugzettenButtonActionListener());
		}
		return zoekBackupTerugzettenButton;
	}

	/**
	 * This method initializes backupTerugzettenButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBackupTerugzettenButton() {
		if (backupTerugzettenButton == null) {
			backupTerugzettenButton = new JButton();
			backupTerugzettenButton.setText("Zet backup terug");
			backupTerugzettenButton.addActionListener(new BackupTerugzettenButtonActionListener());
		}
		return backupTerugzettenButton;
	}

	private Integer getAgeMin(String from, String sex) {
		if( cats.size() == 0 ) {
			// Get Categorie from database in the list of CategorieBeans
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT * FROM Categorie ");
				while( res.next() ) {
					CategorieBean cat = new CategorieBean();
					cat.setCat(res.getString("cat"));
					cat.setSex(res.getString("sex"));
					cat.setVan(res.getInt("van"));
					cat.setTem(res.getInt("tem"));
					cats.add(cat);
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			if( ! hasBegun ) { SConnection.commit(); }
			
		}
		
		for (CategorieBean c : cats) {
			if( from.equalsIgnoreCase(c.getCat()) && (sex.equalsIgnoreCase(c.getSex()) || "A".equalsIgnoreCase(c.getSex())) ) {
				return c.getVan();
			}
		}
		return -1;
	}
	private Integer getAgeMax(String to, String sex) {
		if( cats.size() == 0 ) {
			// Get Categorie from database in the list of CategorieBeans
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			Connection JDBCcon = SConnection.getBegunDBConnection();
			try {
				Statement stmt = JDBCcon.createStatement();
				ResultSet res = stmt.executeQuery("SELECT * FROM Categorie ");
				while( res.next() ) {
					CategorieBean cat = new CategorieBean();
					cat.setCat(res.getString("cat"));
					cat.setSex(res.getString("sex"));
					cat.setVan(res.getInt("van"));
					cat.setTem((res.getInt("tem")==127?-1:res.getInt("tem")));
					cats.add(cat);
				}
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null,
						e.getMessage()+e.getCause(),
						"SQL fout", JOptionPane.ERROR_MESSAGE);
			}
			if( ! hasBegun ) { SConnection.commit(); }
			
		}
		
		for (CategorieBean c : cats) {
			if( to.equalsIgnoreCase(c.getCat()) && (sex.equalsIgnoreCase(c.getSex()) || "A".equalsIgnoreCase(c.getSex())) ) {
				return c.getTem();
			}
		}
		return -1;
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
